
function check_and_run(prog_name)
  local command = ("ps aux | awk '{if ($11 == \""..prog_name.."\")print $2}'")
  local exec = io.popen(command)
  local res = {}
  for i in exec:lines() do
    table.insert(res, i)
  end
  exec:close()

  local cs = 0
  for i = 1, #res do
    cs = cs+1
    if cs > 1 then
      local cmd = ("kill "..res[i])
      os.execute(cmd)
    end
  end
  if cs == 0 then
    command = ("sleep 2 && "..prog_name.." &")
    os.execute(command)
  end
end

function kill_all()
  local command = ("wmctrl -lp | awk '{print $1}'")
  local exec = io.popen(command)
  local cols = 0
  for i in exec:lines() do
    local cmd = ("wmctrl -ic "..i) 
    os.execute(cmd)
    cols = cols + 1
  end
  exec:close()
  return cols
end

function shutdown(cmd)
  local res = kill_all()
  while res ~= 0 do
    res = kill_all()
  end
  os.execute(cmd)
end 

